package asc.biz.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import asc.biz.dao.AscCounselDao;
import asc.biz.service.AscCounselService;
import asc.biz.vo.AscCounsel;

@Service("SfmcCustomerService")
public class AscCounselServiceImpl implements AscCounselService{
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="AscCounselDao")
	private AscCounselDao ascCounselDao;
	
	public void setSfmcCustomer(AscCounselDao ascCounselDao){
		this.ascCounselDao = ascCounselDao;
	}

	public List<AscCounsel> getCounselList(AscCounsel ascCounsel){
		return getCounselList(ascCounsel, false);
	}
	public List<AscCounsel> getCounselList(AscCounsel ascCounsel, boolean isExcel){
		
		String where = "";
		
		try{
			
			if(ascCounsel.getSearchCounselIdx() != 0){
				where = "AND A.counselIdx = " + ascCounsel.getSearchCounselIdx() + "\n";
			}else{
				if(!"".equals(ascCounsel.getCounselType())){
					where += "AND A.counselType LIKE '%" + ascCounsel.getCounselType() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounselor())){
					where += "AND A.counselor LIKE '%" + ascCounsel.getCounselor() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounselee())){
					where += "AND A.counselee LIKE '%" + ascCounsel.getCounselee() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounseleePhone())){
					where += "AND A.counseleePhone LIKE '%" + ascCounsel.getCounseleePhone() + "%'\n";
				}
			}

			if(isExcel){
				where += "ORDER BY A.counselIdx DESC \n";
			}else{
				where += "ORDER BY A.counselIdx DESC LIMIT "+ascCounsel.getOffSet()+", "+ascCounsel.getPageSize()+" \n";
			}
			
			ascCounsel.setWhere(where);
			
			return ascCounselDao.getCounselList(ascCounsel);
			
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}

	public int insertCounsel(AscCounsel dto) {
		try{
			ascCounselDao.insertCounsel(dto);
			return 1;
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}
	
	public int updateCounsel(AscCounsel dto) {
		try{
			return ascCounselDao.updateCounsel(dto);
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}

	public int deleteCounsel(AscCounsel dto) {
		try{
			return ascCounselDao.deleteCounsel(dto);
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}

	@Override
	public int getCounselCount(AscCounsel ascCounsel) {
		String where = "";
		
try{
			
			if(ascCounsel.getSearchCounselIdx() != 0){
				where = "AND A.counselIdx = " + ascCounsel.getSearchCounselIdx() + "\n";
			}else{
				if(!"".equals(ascCounsel.getCounselType())){
					where += "AND A.counselType LIKE '%" + ascCounsel.getCounselType() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounselor())){
					where += "AND A.counselor LIKE '%" + ascCounsel.getCounselor() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounselee())){
					where += "AND A.counselee LIKE '%" + ascCounsel.getCounselee() + "%'\n";
				}
				if(!"".equals(ascCounsel.getCounseleePhone())){
					where += "AND A.counseleePhone LIKE '%" + ascCounsel.getCounseleePhone() + "%'\n";
				}
			}
			
			ascCounsel.setWhere(where);
			
			return ascCounselDao.getCounselCount(ascCounsel);
			
		}catch(Exception e){
			log.error(e);
			return 0;
		}
	}
}
